<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/vue.svg" alt="" />
      </div>

      <!-- 登录表单区域 -->
      <el-form
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginFormRef"
        label-width="80px"
        class="el-form"
      >
        <el-form-item label="用户名" class="info" prop="username">
          <el-input v-model="loginForm.username"> </el-input>
          <i class="iconfont icon-user"></i>
        </el-form-item>

        <el-form-item label="密码" class="info" prop="password">
          <el-input v-model="loginForm.password" type="password"></el-input>
          <i class="iconfont icon-3702mima"></i>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { getCurrentInstance } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter();
const currentInstance = getCurrentInstance();

const { $axios } = currentInstance.appContext.config.globalProperties;

// 登录表单的数据绑定对象
const loginForm = reactive({
  username: 'admin',
  password: '123456',
});
// 登录表单的验证规则 对象
const loginFormRules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 15, message: '长度在 6 到 15  个字符', trigger: 'blur' },
  ],
});

// 获取登录表单
const loginFormRef = ref(null);

// 点击重置按钮 重置表单
function resetLoginForm() {
  console.dir(loginFormRef);
  loginFormRef._value.resetFields();
}

// 点击登录 预验证
const login = () => {
  loginFormRef._value.validate(async (valid) => {
    if (!valid) return;
    const { data: res } = await $axios.post('login', loginForm);
    if (res.meta.status !== 200)
      return ElMessage({
        message: '登录失败！',
        type: 'error',
        showClose: true,
      });
    // 登录成功
    ElMessage({
      message: '登录成功！',
      type: 'success',
      showClose: true,
    });

    window.sessionStorage.setItem('token', res.data.token);
    // 通过编程式导航 到
    router.push('/users');
  });
};
</script>

<style lang="css" scoped>
.login_container {
  background-color: #264a6b;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login_box {
  width: 90%;
  max-width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  display: flex;
  justify-content: center;
  position: relative;
}
.login_box .avatar_box {
  position: absolute;
  top: -75px;
  box-sizing: border-box;
  background-color: transparent;
  height: 130px;
  padding-top: 20px;
  width: 130px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 1px 3px 6px rgba(0, 0, 0, 0.1), -2px -3px 6px rgba(0, 0, 0, 0.1);
}
.login_box .avatar_box img {
  width: 80%;
  height: auto;
}

.el-form {
  box-sizing: border-box;
  bottom: 0;
  position: absolute;
  width: 100%;
  padding: 0 20px;
}

.btns {
  float: right;
}

.info label {
  color: #555;
  font-size: 18px;
}

.iconfont {
  position: absolute;
  right: 10px;
  color: #777;
}
</style>
